package com.dajie.pt.service.utils;

import com.dajie.core.cv.model.ResumePractice;
import com.dajie.core.cv.service.ResumeService;
import com.dajie.core.profile.ProfileServicesContext;
import com.dajie.core.profile.enums.ComputedIdentityEnum;
import com.dajie.core.profile.model.*;
import com.dajie.core.profile.service.CardInfoService;
import com.dajie.core.profile.service.ComputedIdentityService;
import com.dajie.core.profile.service.ProfileService;
import com.dajie.corp.api.CorpServicesContext;
import com.dajie.corp.api.service.CorpJobService;
import com.dajie.corp.api.service.CorpProjectService;
import com.dajie.corp.api.service.CorpScheduleService;
import com.dajie.corp.api.service.CorpService;
import com.dajie.corp.biz.project.model.ClubProject;
import com.dajie.corp.biz.schedule.model.Schedule;
import com.dajie.corp.enums.common.CorpStatusEnum;
import com.dajie.corp.info.model.CorpBase;
import com.dajie.infra.user.InfraUserServiceContext;
import com.dajie.infra.user.model.UserBase;
import com.dajie.infra.user.model.UserPhone;
import com.dajie.infra.user.service.UserBaseService;
import com.dajie.job.v2.client.JobServicesContext;
import com.dajie.job.v2.service.JobQueryService;
import com.dajie.job.v2.service.result.JobView;
import com.dajie.job.v2.vo.CorpVo;
import com.dajie.resume.ResumeServiceContext;


import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by yunbinan on 15-6-11.
 */
public class CommonUtil {
    public static final ComputedIdentityService computedIdentityService = ProfileServicesContext.getInstance().getComputedIdentityService();
    public static final ProfileService profileService = ProfileServicesContext.getInstance().getProfileService();
    public static final UserBaseService userService = InfraUserServiceContext.getInstance().getUserService();
    public static final CardInfoService cardInfoService = ProfileServicesContext.getInstance().getCardService();
    public static final JobQueryService jobQueryService = JobServicesContext.getInstance().getJobQueryService();
    public static final CorpJobService corpJobService = CorpServicesContext.getInstance().getCorpJobService();
    public static final CorpScheduleService corpScheduleService = CorpServicesContext.getInstance().getCorpScheduleService();
    public static final CorpProjectService corpProjectService = CorpServicesContext.getInstance().getCorpProjectService();
    public static final CorpService corpService = CorpServicesContext.getInstance().getCorpService();
    public static Pattern pattern = Pattern.compile("^1\\d{10}$");
    public static final ResumeService resumeService = ResumeServiceContext.getInstance().getResumeService();

    public static int getIdentityByUid(int uid) {
        /**
         * 1：白领
         * 2：准白领
         * 3：应届生
         * 4：非应届生
         **/
        ComputedIdentityEnum computedIdentityEnum = computedIdentityService.getIdentity(uid);
        UserIdentity userIdentity = profileService.getUserIdentityByUid(uid);
        userIdentity.getIdentityType();
        return computedIdentityEnum.getCode();
    }

    public static String getUnameByUid(int uid) {
        String name = null;
        UserBase userBase = userService.getUserByUid(uid);
        //判断用户是否注销
        if (userBase != null && !userBase.isCanceled() && !userBase.isDisabled()) {
            name = userBase.getName();
        }
        return name;
    }

    public static String getMobileByUid(int uid) {
        UserPhone userPhone = userService.getDefaultPhoneByUid(uid);
        UserDetail userDetail1 = null;
        String mobile = "";
        if (userPhone == null) {
            userDetail1 = profileService.getUserDetailByUid(uid);
            mobile = userDetail1.getMobile();
        } else {
            mobile = userPhone.getPhoneNumber();
        }
        if (mobile.contains("-")) {
            String[] t_mobile = mobile.split("-");
            if (t_mobile.length > 1) {
                mobile = mobile.split("-")[1];
            }
        }
        if (!pattern.matcher(mobile).find()) {
            mobile = "";
        }
        return mobile;
    }

    public static String getCorpNameByUid(int uid) {
        String corpName = null;
        List<WorkExperience> workExperience = profileService.getWorkExperienceListByUid(uid);
        if (workExperience != null && workExperience.size() > 0) {
            corpName = workExperience.get(0).getCorpName();
            workExperience.get(0).getJobCity();
            workExperience.get(0).getPosition();

        }
        if (corpName == null) {
            ResumePractice resumePractice = resumeService.getLatestFulltimePracticeByUid(uid);
            if (resumePractice != null)
                corpName = resumePractice.getCorpName();
        }
        return corpName;
    }

    public static String getSchoolNameByUid(int uid) {
        String school = null;
        Education education = profileService.getHighestEducationByUid(uid);
        if (education != null) {
            school = education.getSchoolName();
        }
        return school;
    }

    public static String getMajorNameByUid(int uid) {
        String edu = "";
        Education education = profileService.getHighestEducationByUid(uid);
        if (education != null) {
            edu = education.getMajorName();
        }
        return edu;
    }

    public static int getWorkYearByUid(int uid) {
        int workYear = 0;
        UserDetail userDetail = profileService.getUserDetailByUid(uid);
        if (userDetail != null) {
            int startYear = userDetail.getWorkStartYear();
            if (startYear != 0) {
                workYear = Calendar.getInstance().get(Calendar.YEAR) - startYear;
            }
        }
        if (workYear == 0)
            workYear = 1;
        return workYear;
    }


    public static String smsUrl(String args) {
        String url = "http://www.dajie.com/app/phone/download?id=5";
        if (url.indexOf("?") > 0) {
            url = url + "&" + args;
        } else {
            url = url + "?" + args;
        }
        return url;
    }

    public static String smsUrlForHr(String args) {
        // String url = "http://m.dajie.com/recruit/candidate/unread?noAppTip=1";
        /*
        2016-05-09更改模板号10001、10002、10003的短信推广链接地址改为：
         */
        String url = "http://www.dajie.com/app/businessphone/download?id=44";
        if (url.indexOf("?") > 0) {
            url = url + "&" + args;
        } else {
            url = url + "?" + args;
        }
        return url;
    }

    public static String smsUrlForProject(String args) {
        String url = "http://m.dajie.com/invitation/unhandled/project";
        if (url.indexOf("?") > 0) {
            url = url + "&" + args;
        } else {
            url = url + "?" + args;
        }
        return url;
    }

    public static String smsUrlForJob(String args) {
        String url = "http://m.dajie.com/invitation/unhandled";
        if (url.indexOf("?") > 0) {
            url = url + "&" + args;
        } else {
            url = url + "?" + args;
        }
        return url;
    }

    public static String smsUrlForSchedule(String args) {
        String url = "http://m.dajie.com/invitation/unhandled/schedule";
        if (url.indexOf("?") > 0) {
            url = url + "&" + args;
        } else {
            url = url + "?" + args;
        }
        return url;
    }

    public static String getCorpNameByJid(String jid) {
        String corpName = "";
        CorpBase corpBase = null;
        JobView jobView = jobQueryService.getByJid(jid);
        if (jobView != null) {
            CorpVo corpVo = jobView.getCorpInfo();
            if (corpVo != null) {
                corpName = corpVo.getName();
                corpBase = corpService.getCorpBaseByName(corpVo.getName());
                if (corpBase != null && corpBase.getAliases() != null) {
                    String corpAlias = corpBase.getAliases().get(0);
                    for (String alias : corpBase.getAliases()) {
                        if (alias.length() < corpAlias.length())
                            corpAlias = alias;
                    }
                    corpName = corpAlias;
                }
            }
        }
        return corpName;
    }

    public static String getCorpNameByPid(int pid) {
        String corpName = "";
        ClubProject clubProject = corpProjectService.findById(pid);
        if (clubProject != null) {
            corpName = clubProject.getCorpName();
            CorpBase corpBase = corpService.getCorpBaseByCid(clubProject.getCid());
            if (corpBase != null && corpBase.getAliases() != null) {
                String corpAlias = corpBase.getAliases().get(0);
                for (String alias : corpBase.getAliases()) {
                    if (alias.length() < corpAlias.length())
                        corpAlias = alias;
                }
                corpName = corpAlias;
            }
        }
        return corpName;
    }

    public static String getCorpNameBySid(int sid) {
        String corpName = "";
        Schedule schedule = corpScheduleService.getScheduleByIdFormDB(sid);
        if (schedule != null) {
            CorpBase corpBase = corpService.getCorpBaseByCid(schedule.getOwnId());
            corpName = corpBase.getName();
            if (corpBase != null && corpBase.getAliases() != null) {
                String corpAlias = corpBase.getAliases().get(0);
                for (String alias : corpBase.getAliases()) {
                    if (alias.length() < corpAlias.length())
                        corpAlias = alias;
                }
                corpName = corpAlias;
            }
        }
        return corpName;
    }

    public static String getCidByJid(String jid) {
        String cid = "";
        JobView jobView = jobQueryService.getByJid(jid);
        if (jobView != null) {
            CorpVo corpVo = jobView.getCorpInfo();
            if (corpVo != null) {
                cid = corpVo.getCid();
            }
        }
        return cid;
    }

    public static String getCidByPid(int pid) {
        String cid = "";
        ClubProject clubProject = corpProjectService.findById(pid);
        if (clubProject != null) {
            cid = clubProject.getCid();
        }
        return cid;
    }

    public static String getCidBySid(int sid) {
        String cid = "";
        Schedule schedule = corpScheduleService.getScheduleByIdFormDB(sid);
        if (schedule != null) {
            CorpBase corpBase = corpService.getCorpBaseByCid(schedule.getOwnId());
            cid = corpBase.getCid();
        }
        return cid;
    }

    public static Boolean isHaveEmail(int uid) {
        if (userService.getEmailsByUid(uid) != null && userService.getEmailsByUid(uid).size() > 0) {
            return true;
        }
        return false;

    }

    public static void main(String[] args) {
    }
}
